package com.example.easyutils;

import androidx.annotation.NonNull;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.example.easyutils.curstartfortune.StartFortune;
import com.example.easyutils.dedecms.Decms;
import com.example.easyutils.food.FoodCook;
import com.example.easyutils.garbagepage.Main;
import com.example.easyutils.httputil.HttpGetRequest;
import com.example.easyutils.joke.JokeActivity;
import com.example.easyutils.logistics.TraceActivity;
import com.example.easyutils.mainpage.GridAdapter;
import com.example.easyutils.mainpage.GridUtil;
import com.example.easyutils.musicpage.MusicType;
import com.example.easyutils.openprize.OpenCode;
import com.example.easyutils.poetry.PoetryActivity;
import com.example.easyutils.translation.TranslationActivity;
import com.example.easyutils.travel.TravelActivity;
import com.example.easyutils.watermark.ClearWatermark;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.example.easyutils.dram.DramActivity;
import okhttp3.Call;
import okhttp3.Response;


public class MainActivity extends BaseActivity {
    private TextView randomPansel;
    private GridView gridViewPage;
    private List itemList;
    private static final int RANDOM_QUOTER = 1;
    private String randomQuoter;
    Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case RANDOM_QUOTER:
                    randomPansel.setText(randomQuoter);
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gridViewPage = (GridView) findViewById(R.id.gridViewPage);
        randomPansel = (TextView)findViewById(R.id.randomPansel);
        itemList = new ArrayList();
        String content_text[] = {"垃圾分类","历史上的今天","今日运势","音乐","视频去水印","菜谱","快递查询","笑话大全","翻译","周公解梦","藏头诗",
        "旅游","彩票","仿站"};
        String id_str[] = {"001","002","003","004","005","006","007","008","009","010","011","012","013","014"};
        int image[] = {R.drawable.garbage1,R.drawable.history,R.drawable.luckey,R.drawable.music,
                R.drawable.water,R.drawable.caipu,R.drawable.kuaidichaxun,R.drawable.xiaohua,R.drawable.translation,
        R.drawable.jiemeng,R.drawable.shici,R.drawable.lvyou,R.drawable.jiang,R.drawable.dedecms};

        for(int i = 0; i < content_text.length; i ++){
            int images = image[i];
            String name = content_text[i];
            String id = id_str[i];
            GridUtil gridUtil = new GridUtil(images,name,id);
            itemList.add(gridUtil);
        }

        GridAdapter gridAdapter = new GridAdapter(MainActivity.this, itemList);
        gridViewPage.setAdapter(gridAdapter);

        gridViewPage.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                switch (position){
                    case 0://垃圾分类
                        Intent intent = new Intent(MainActivity.this, Main.class);
                        startActivity(intent);
                        break;
                    case 1://历史上的今天
                        Intent intent1 = new Intent(MainActivity.this,HistoryToday.class);
                        startActivity(intent1);
                        break;
                    case 2://星座
                        Intent intent2 = new Intent(MainActivity.this, StartFortune.class);
                        startActivity(intent2);
                        break;
                    case 3://音乐
                        Intent intent3 = new Intent(MainActivity.this, MusicType.class);
                        startActivity(intent3);
                        break;
                    case 4://去水印
                        Intent intent4 = new Intent(MainActivity.this, ClearWatermark.class);
                        startActivity(intent4);
                        break;
                    case 5://菜谱
                        Intent intent5 = new Intent(MainActivity.this, FoodCook.class);
                        startActivity(intent5);
                        break;
                    case 6://快递查询
                        Intent intent6 = new Intent(MainActivity.this, TraceActivity.class);
                        startActivity(intent6);
                        break;
                    case 7://笑话大全
                        Intent intent7 = new Intent(MainActivity.this, JokeActivity.class);
                        startActivity(intent7);
                        break;
                    case 8://翻译
                        Intent intent8 = new Intent(MainActivity.this, TranslationActivity.class);
                        startActivity(intent8);
                        break;
                    case 9://解梦
                        Intent intent9 = new Intent(MainActivity.this, DramActivity.class);
                        startActivity(intent9);
                        break;
                    case 10://藏头诗
                        Intent intent10 = new Intent(MainActivity.this, PoetryActivity.class);
                        startActivity(intent10);
                        break;
                    case 11://旅游
                        Intent intent11 = new Intent(MainActivity.this, TravelActivity.class);
                        startActivity(intent11);
                        break;
                    case 12://彩票
                        Intent intent12 = new Intent(MainActivity.this, OpenCode.class);
                        startActivity(intent12);
                        break;
                    case 13://仿站
                        Intent intent13 = new Intent(MainActivity.this, Decms.class);
                        startActivity(intent13);
                        break;
                    default:
                        throw new IllegalStateException("Unexpected value: " + position);
                }
            }
        });
        getRandomQuotations();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {//监听返回键
        Log.d("返回键",keyCode+"");
        if(keyCode == KeyEvent.KEYCODE_BACK){
            moveTaskToBack(true);
//            Intent intent = new Intent();
//            intent.setClass(MainActivity.this, MusicType.class);
//            intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);//设置不要刷新将要跳到的界面
//            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//它可以关掉所要到的界面中间的activity
//            startActivity(intent);
//            finish();

            return true;
        }
        return super.onKeyDown(keyCode, event);

    }


    public void getRandomQuotations(){//获取随机语录
        HttpGetRequest.sendOkHttpGetRequst("http://api.63code.com/yan/api.php",
                new okhttp3.Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Log.d("error","出错");
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String responseData = response.body().string();
                        randomQuoter = responseData;
                        handler.sendEmptyMessage(RANDOM_QUOTER);
                        Log.d("成功",responseData);
                    }
                });
    }

}